package com.lbssoft.gpsbd.controller;

import com.lbssoft.gpsbd.dao.PositionMapper;
import com.lbssoft.gpsbd.model.Position;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.PositionStatus;
import com.lbssoft.gpsbd.model.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api("位置查询")
@RestController
@RequestMapping(value="/positions")
@Validated
public class PositionController {
	@Autowired
	PositionMapper positionMapper;


	@ApiOperation(value="查询位置", notes="获取用户下所有设备的最后位置")
	@RequestMapping(value = "",method = {RequestMethod.GET})
	@ResponseBody
	public Result last(HttpServletRequest request) {
		Result result = new Result();
		UserVo user = (UserVo)SecurityUtils.getSubject().getPrincipal();
		List<PositionStatus> positions = positionMapper.getLastPostionByUid(user.getId());
		result.setCode(0);
		result.setData(positions);
		return result;
	}
	

	@ApiOperation(value="历史轨迹", notes="获取设备指定时间段的历史轨迹")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",name = "id", value = "设备id", required = true, dataType = "int"),
		@ApiImplicitParam(paramType="query",name = "start", value = "开始时间", required = true, dataType = "String"),
		@ApiImplicitParam(paramType="query",name = "end", value = "结束时间", required = true, dataType = "String")
	})
	@RequestMapping(value = "/history",method = {RequestMethod.GET})
	@ResponseBody
	public Result history(
			@NotEmpty @RequestParam(value = "id", defaultValue = "") String id,
			@NotEmpty @RequestParam(value = "start", defaultValue = "") String start,
			@NotEmpty @RequestParam(value = "end", defaultValue = "") String end
			
			) 
	{
		Result result = new Result();
		List<Position> positions = positionMapper.getHistory(start, end, Integer.parseInt(id));
		result.setCode(0);
		result.setData(positions);

		return result;
	}
	
}